/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package nhadat.action.form;

import java.io.FileOutputStream;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import nhadat.common.CommonConstant;
import nhadat.dao.TindangDao;
import nhadat.entity.TblTinDang;
import nhadat.tindangfull.TindangfullType;
import nhadat.xml.DatabaseAndXMLFileMappingSyncrolization;
import nhadat.xml.StaxParser;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

/**
 *
 * @author huanht
 */
public class GetPDFActionForm extends org.apache.struts.action.ActionForm {
    
   private String msts;

    public String getMsts() {
        return msts;
    }

    public void setMsts(String msts) {
        this.msts = msts;
    }

    public TblTinDang xemChiTiet() {
        TindangDao tindang = new TindangDao();
        StaxParser reader = new StaxParser();
        String path = getServlet().getServletContext().getRealPath("/");

        int tmp = Integer.parseInt(getServlet().getServletContext().getAttribute(CommonConstant.MAX_MATINDANG_DATABASE).toString());
        if (Integer.parseInt(msts) <= tmp) {
            TblTinDang[] result = tindang.getAllTindangByID(msts);

            if (result != null) {
                return result[0];
            }
        } else {
            List<TindangfullType> result = reader.readXMLtoTindangFullTypebyStAX(path + "xml/TindangFull.xml");
            for (int i = 0; i< result.size(); i++) {
                if (msts.equals(result.get(i).getMatindang())) {
                    String loainhaFullXML = path + "xml/TblLoaiNha.xml";
                    String loaigiaytoFullXML = path + "xml/TblLoaiGiayTo.xml";
                    String khuvucFullXML = path + "xml/TblKhuVuc.xml";
                    String loaitinFullXML = path + "xml/TblLoaiTin.xml";

                    DatabaseAndXMLFileMappingSyncrolization syn = new DatabaseAndXMLFileMappingSyncrolization();
                    TblTinDang tin = syn.mapTblTinDangWithTindangFullType(result.get(i), loainhaFullXML, loaigiaytoFullXML, khuvucFullXML, loaitinFullXML);

                    return tin;
                }
            }
        }

        return null;
    }

    public void methodTrax(String xslPath, String xmlPath, String output) {
        try {
            TransformerFactory tf = TransformerFactory.newInstance();
            StreamSource xsltFile = new StreamSource(xslPath);
            Transformer tran = tf.newTransformer(xsltFile);
            StreamSource xmlFile = new StreamSource(xmlPath);
            StreamResult htmlFile = new StreamResult(new FileOutputStream(output));
            tran.setOutputProperty(OutputKeys.ENCODING, "UTF-8");

            
            tran.transform(xmlFile, htmlFile);

        } catch(TransformerConfigurationException e) {
            e.printStackTrace();
        } catch(Exception e) {
            e.printStackTrace();
        }
    }
    public GetPDFActionForm() {
        super();
        // TODO Auto-generated constructor stub
    }

}
